package visualizeData;

import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class VisualizeConnectionWeightPlots extends JPanel {

	public VisualizeConnectionWeightPlots(String chartName, String xLabel,
			String yLabel) {
		super();

		int hiddenLayers = 1;
		int hiddenNeurons = 4;
		int totalNeurons = 1 + hiddenLayers * hiddenNeurons;
		int totalConnection = 4;

		XYSeries series[] = new XYSeries[totalNeurons];

		int size = 600;
		int j = 0;

		series = createWeightSeries(0, 0, "Output Layer");
		System.out.println(series[0].getMaxY());
		
//		System.out.println(" created series:" + 0);
//		
//		for (int hl=0;hl<series[0].getMaxX();hl++){
//			System.out.println(hl);
//			System.out.println(hl+","+series[1].getDataItem(hl));
//		}
		// for (int hl=1;hl<=hiddenLayers;hl++){
		// j++;
		// for (int hn=0;hn<hiddenNeurons;hn++){
		// System.out.println(" created series:"+ (j+hn));
		// series[j+hn] = createWeightSeries(hl, hn, "Hidden Layer:"+hn);
		//
		// }
		// }

		// XYSeriesCollection dataset[][] = new
		// XYSeriesCollection[totalNeurons][totalConnection];
		ChartPanel chartPanel[] = new ChartPanel[totalNeurons];
		JFreeChart chart[] = new JFreeChart[totalNeurons];

		XYSeriesCollection dataset[] = new XYSeriesCollection[4];
		
//		dataset[0] = new XYSeriesCollection();
//		dataset[0].addSeries(series[0]);
//		chart[0] = ChartFactory.createXYLineChart(
//				series[0].getDescription(), xLabel, yLabel, dataset[0],
//				PlotOrientation.VERTICAL, true, true, true);
//		chartPanel[0] = new ChartPanel(chart[0], size, size, size, size,
//				size, size, true, true, true, true, true, true, true);
//		add(chartPanel[0]);
		
		for (int n = 0; n < 4; n++) {
			System.out.println(" series length:" + n);
			dataset[n] = new XYSeriesCollection();
			dataset[n].addSeries(series[n]);
			chart[n] = ChartFactory.createXYLineChart(
					series[n].getDescription(), xLabel, yLabel, dataset[n],
					PlotOrientation.VERTICAL, true, true, true);
			chartPanel[n] = new ChartPanel(chart[n], size, size, size, size,
					size, size, true, true, true, true, true, true, true);
			add(chartPanel[n]);
		}

	}

	private XYSeries[] createWeightSeries(int n1, int n2, String seriesName) {
		XYSeries[] series = new XYSeries[4];
		try {

			String fFileName = "Log/neuralnetworks.log";

			Scanner scanner = new Scanner(new FileInputStream(fFileName));
			Double[] lastweight = new Double[] {0d,0d,0d,0d};
			
			series[0] = new XYSeries(seriesName+"0");
			series[1] = new XYSeries(seriesName+"1");
			series[2] = new XYSeries(seriesName+"2");
			series[3] = new XYSeries(seriesName+"3");

			while (scanner.hasNextLine()) {
				String m1 = scanner.nextLine();

				if (m1.contains("Output Layer") && m1.contains("INPUT:  [1.0, 0.0]")) {
					String[] m3 = m1.split("\\|");

					String[] m2 = m3[2].split(",");

					if (Integer.valueOf(m2[1]) == n1
							&& Integer.valueOf(m2[2]) == n2) {
						String weightsplit1 = m1.split("\\[")[3];
						String weightsplit = weightsplit1.split("]")[0];
						String[] weightsplit2 = weightsplit.split(",");

						 System.out.println(Integer.valueOf(m2[0]) + ","
						 + Double.valueOf(m2[3]));
						// series.add(Integer.valueOf(m2[0]),
						// Double.valueOf(m2[3]));

						for (int n = 0; n < weightsplit2.length; n++) {
							System.out.println(n+","+Integer.valueOf(m2[0])+","+weightsplit2[n]);
								series[n].add(Integer.valueOf(m2[0]),
										Double.valueOf(weightsplit2[n]));
//							System.out.println(series[n].getDataItem(Integer.valueOf(m2[0])-1));
						}
					}
				}
			}
			scanner.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return series;

	}

	public static void main(String[] args) {

		JPanel panel = new JPanel();
		// panel.setBounds(new Rectangle(1000,1024));
		JFrame frame = new JFrame("Algorithm Performance Stats");

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		frame.add(panel);
		//
		// frame.add(scrollBar);
		VisualizeConnectionWeightPlots VeP = new VisualizeConnectionWeightPlots(
				"Weigth Error Data", "xLabel", "yLabel");
		// p.setVisible(true);
		JScrollPane scrollBar = new JScrollPane(VeP,
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

		frame.getContentPane().add(scrollBar, BorderLayout.CENTER);

		frame.pack();
		frame.setVisible(true);
	}
}